package com.guldan.thrall.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guldan.core.controller.BaseController;
import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.response.ResponseJson;
import com.guldan.jaina.dto.area.AreaDTO;
import com.guldan.jaina.dto.dicType.DicTypeDTO;
import com.guldan.jaina.dto.dicType.DicTypeSearchDTO;
import com.guldan.jaina.dto.dictionary.DictionaryDTO;
import com.guldan.jaina.dto.dictionary.DictionarySearchDTO;
import com.guldan.jaina.provider.IDicTypeProvider;
import com.guldan.jaina.provider.IDictionaryProvider;

/**
 * Created by Silas.
 * Date: 2016/11/10
 * Time: 22:06
 */
@Controller
@RequestMapping("/dic")
public class DictionaryController extends BaseController {


    @Autowired
    private IDictionaryProvider iDictionaryProvider;

    @Autowired
    private IDicTypeProvider iDicTypeProvider;

    /**
     * 获取字典类别列表
     *
     * @return
     */
    @RequestMapping(value = "/typeList", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<DicTypeDTO> typeList() {
        DicTypeSearchDTO dicTypeSearchDTO = new DicTypeSearchDTO();
        ResponseDTO<List<DicTypeDTO>> response = iDicTypeProvider.all(dicTypeSearchDTO);
        return response.getData();
    }

    /**
     * 根据查询条件获取字典列表
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/dicList", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    List<DictionaryDTO> dicList(@RequestBody DictionarySearchDTO dto) {
        ResponseDTO<List<DictionaryDTO>> response = iDictionaryProvider.all(dto);
        return response.getData();
    }

    /**
     * 新增/保存字典类别
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/typeSave", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    DicTypeDTO typeSave(@RequestBody DicTypeDTO dto) throws Exception {
        ResponseDTO<DicTypeDTO> response = iDicTypeProvider.save(dto);
        if (response.isSuccess())
            return response.getData();
        else {
            throw response.getException();
        }

    }

    /**
     * 新增/保存字典
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/dicSave", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    DictionaryDTO dicSave(@RequestBody DictionaryDTO dto) throws Exception {
        ResponseDTO<DictionaryDTO> response = iDictionaryProvider.save(dto);
        if (response.isSuccess())
            return response.getData();
        else {
            throw response.getException();
        }
    }

    /**
     * 字典类别删除
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/typeDelete", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Boolean typeDelete(@RequestBody DicTypeDTO dto) {
        ResponseDTO<Boolean> response = iDicTypeProvider.delete(dto.getDicTypeId());
        return response.getData();
    }

    /**
     * 字典删除
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/dicDelete", method = RequestMethod.POST)
    @ResponseJson
    public
    @ResponseBody
    Boolean dicDelete(@RequestBody DictionaryDTO dto) {
        ResponseDTO<Boolean> response = iDictionaryProvider.delete(dto.getDictionaryId());
        return response.getData();
    }
}
